package org.tof.hotel.entity;

import org.apache.log4j.Logger;
import org.tof.hotel.Generator;
import org.tof.hotel.csv.CSVParser;
import org.tof.hotel.csv.CSVWriter;

import java.util.ArrayList;
import java.util.Random;
import java.util.UUID;

/**
 * A class which stores information about Customer.
 *
 * @author A. Timofieiev
 */

public class Customer extends Person {
    public Customer(String name, String phone, String email) {
        super(name, phone, email);
        customers.add(this);
        logger.debug("generated object of class \"Customer\"");
    }

    static Logger logger = Logger.getLogger(Customer.class.getName());
    public static ArrayList<Customer> customers = new ArrayList<Customer>();

    // -----methods----- //

    /**
     * Returns all applications filled by the customer.
     *
     * @return <code>ArrayList<Application></code> if there are applications, filled by this customer; <code>null</code> otherwise
     * @see Application
     */
    public ArrayList<Application> getApplications() {
        return Application.byCustomer(this);
    }

    public static Customer getById(UUID id){
        for(Customer c : customers){
            if(c.getId() == id){
                return c;
            }
        }
        logger.warn("No Customer with such id:" + id);
        return null;
    }

    // ----- GeneratoR ----- //

    @Override
    public void generate(int num) {
        logger.debug("generating " + num + " objects of class \"Customer\"");            
        for (int i = 1; i <= num; i++) {
            new Customer(Generator.names[(new Random()).nextInt(6)], "+380 44 777 77 77", "b.gates@gmail.com");
        }
    }
    // ----- CSVGenerable-generator ----- //

    @Override
    public ArrayList<String> getFields() {
        ArrayList<String> list = new ArrayList<String>();
        ArrayList<String> tmp = new ArrayList<String>();
        for (Customer c : customers) {
            tmp.clear();
            tmp.add(c.getId().toString());
            tmp.add(c.getName());
            tmp.add(c.getPhone());
            tmp.add(c.getEmail());
            list.add(CSVWriter.getInstance().FieldsToCSVString(tmp));
        }
        return list;
    }

    @Override
    public ArrayList<Class> createInstancesFromCsvFile(String filename) {
        CSVParser parser = new CSVParser();
        ArrayList<String[]> list = new ArrayList<String[]>(parser.parse(filename));
        ArrayList<Class> generated = new ArrayList<Class>();
        for(int i=1; i < list.size() ;i++){
            generated.add(new Customer(list.get(i)[1],list.get(i)[2],list.get(i)[3]).getClass());
        }
        return generated;
    }

}
